﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImageProcessor.Tim
{
    /// <summary>
    /// Interaction logic for HistogramControl.xaml
    /// </summary>
    public partial class HistogramControl : UserControl
    {
        public static readonly DependencyProperty ChanelValuesProperty =
            DependencyProperty.Register("ChanelValues", typeof(int[]), typeof(HistogramControl), new PropertyMetadata(default(int[])) { PropertyChangedCallback = ChangelValuesPropertyChanged });

        public static readonly DependencyProperty FillProperty =
            DependencyProperty.Register("Fill", typeof (Brush), typeof (HistogramControl), new PropertyMetadata(default(Brush)){PropertyChangedCallback = FillPropertyChanged});

        public static readonly DependencyProperty AxisYMaxValueProperty =
            DependencyProperty.Register("AxisYMaxValue", typeof (double), typeof (HistogramControl), new PropertyMetadata(default(double)));

        private static void FillPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((HistogramControl)d).Refresh();
        }

        private static void ChangelValuesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((HistogramControl)d).Refresh();
        }

        public HistogramControl()
        {
            InitializeComponent();
        }

        public int[] ChanelValues
        {
            get { return (int[]) GetValue(ChanelValuesProperty); }
            set { SetValue(ChanelValuesProperty, value); }
        }

        public Brush Fill
        {
            get { return (Brush)GetValue(FillProperty); }
            set { SetValue(FillProperty, value); }
        }

        public double AxisYMaxValue
        {
            get { return (double)GetValue(AxisYMaxValueProperty); }
            set { SetValue(AxisYMaxValueProperty, value); }
        }

        private void Refresh()
        {
            _canvas.Children.Clear();

            if (ChanelValues == null || Fill == null)
                return;

            for (var i = 0; i < ChanelValues.Length; i++)
            {
                var value = ChanelValues[i];
                var line = new Line
                               {
                                   X1 = i,
                                   X2 = i,
                                   Y1 = 0,
                                   Y2 = value,
                                   Fill = Fill,
                                   Stroke = Fill
                               };
                _canvas.Children.Add(line);
            }
        }

    }
}
